package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.base.BaseController;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.Group;
import com.yxh.pojo.Unit;
import com.yxh.pojo.vo.GroupMenuVO;
import com.yxh.service.GroupService;
import com.yxh.util.APIErrorCode;
import com.yxh.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import static com.yxh.util.APIErrorCode.DELETE_ERROR;
import static com.yxh.util.APIErrorCode.*;

/**
 * Created by asus on 2018/5/30.
 */

@Scope("prototype")
@RestController
@RequestMapping(value = "/group/")
public class GroupController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GroupController.class);
    private Response response;

    @Autowired
    private GroupService groupService;


    /**
     * 保存角色
     *
     * @param group
     * @return
     */
    @PostMapping("save_group")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveGroup(@RequestBody Group group) {

        //System.out.println(SnowflakeIdWorker.creatId());
        //  group.setId(SnowflakeIdWorker.creatId());
        group.setType("1");
        Group data = groupService.get(group);
        if (data != null) {
            response = Response.returnResponse(APIErrorCode.ROLE_EXIST);
            return response;
        }
        response = Response.returnResponse(APIErrorCode.SUCCESS);
        response.setData(groupService.save(group));
        return response;
    }

    /**
     * 保存角色菜单权限
     *
     * @param groupMenuVO
     * @return
     */
    @PostMapping("save_group_menu")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveGroupMenu(@RequestBody GroupMenuVO groupMenuVO) {
        groupService.saveGroupMenu(groupMenuVO);
        response = Response.returnResponse(APIErrorCode.SUCCESS);
        //response.setData();
        return response;
    }


    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response removeGroup(@PathVariable("id") String id) {
        groupService.delete(id);
        return Response.returnResponse(SUCCESS);
    }


    /**
     * 删除角色菜单权限
     *
     * @param id
     * @return
     */
    @DeleteMapping("group_menu/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response removeGroupMenu(@PathVariable("id") String id) {
        groupService.delete(id);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * 更新橘色
     *
     * @param deptJson
     * @return
     */
    @PutMapping("update_groupInfo")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response updateGroup(@RequestBody Group deptJson) {
        int i = groupService.update(deptJson);
        if (i == 0)
            return Response.returnResponse(OPERATION_ERROR);
        else
            return Response.returnResponse(SUCCESS);
    }


    /**
     * 查询角色
     *
     * @param deptJson
     * @return
     */
    @GetMapping("get_information")
   // @Authorization
    @ResponseBody
    public Response getInformation(Group deptJson, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = groupService.getInformation(deptJson, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);
        return response;
    }


    /**
     * 查询角色菜单权限
     *
     * @param group
     * @return
     */
    @GetMapping("get_information_group_menu")
    @Authorization
    @ResponseBody
    public Response getInformationGroupMenu(Group group, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = groupService.getInformationGroupMenu(group, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);
        return response;
    }

}
